<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;
use app\common\model\user\UserGroup;


class QueryNotis extends Command
{
    protected function configure()
    {
        $this->setName('query_refund')
            ->setDescription('通知消息处理');
    }


    protected function execute(Input $input, Output $output)
    {
        try {
            $userNotisLowBalance = $list = Db::name('userNotisSetting')->where('low_balance_enable', 1)
//                ->field('low_balance,low_balance_enable')
                ->select()->toArray();
            file_put_contents('userNotisSetting.txt', date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND);
            file_put_contents('userNotisSetting.txt', json_encode($userNotisLowBalance).PHP_EOL, FILE_APPEND);

            if (empty($userNotisLowBalance)) {
                return false;
            }

            foreach ($userNotisLowBalance as $value) {
                $this->checkLowBalance($value);
            }
            $output->writeln('通知消息处理成功');

            return true;
        } catch (\Exception $e) {
            Log::write('失败原因:' . $e->getMessage());
            return false;
        }
    }

    //低余额检测
    public function checkLowBalance($data)
    {
        $user_money = UserGroup::where('id', $data['group_id'])->value('user_money');
        $where = [
            'group_id' => $data['group_id'],
            'type' => 1,
        ];
        if ($user_money < $data['low_balance']) {
            $record = Db::name('userNotis')->where('user_id', $data['user_id'])->where('type', 1)->find();
            $data = [
                'type' => 1,
                'user_id' => $data['user_id'],
                'group_id'=> $data['group_id'],
                'is_read' => 00,
                'msg' => '您的余额已低于' . $data['low_balance'] . '元，请及时充值',
                'create_time' => time(),
            ];
            if (empty($record)) {
                // 插入记录
                Db::name('userNotis')->insert($data);
            }
        } else {
            return false;
        }

    }


}